#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{    
    roiChecked = false;
    rectChecked = false;
    circleChecked = false;
    lineChecked = false;

    // 4 menus for the softWare
    menuFiles = menuBar()->addMenu("&Fichier");
    menuImages = menuBar()->addMenu("&Images");
    menuSelect = menuBar()->addMenu("&Selection");
    menuHelp = menuBar()->addMenu("&Aide");

    // Files menu
    openVideo = menuFiles->addAction("Ouvrir Video");
    openVideo->setShortcut(QKeySequence("Ctrl+O"));
    openVideo->setIcon((QIcon("image/video.png")));
    openConfig = menuFiles->addAction("Ouvrir configuration");
    openConfig->setIcon(QIcon("image/open.png"));
    saveConfig = menuFiles->addAction("Sauvegarder configuration");
    saveConfig->setIcon(QIcon("image/saveConfig.png"));
    quit = menuFiles->addAction("&Quitter");
    quit->setShortcut(QKeySequence("Ctrl+Q"));
    quit->setIcon(QIcon("image/eteindre.png"));
    // Image menu
    saveImage = menuImages->addAction("Sauvegarder image");
    saveImage->setShortcut(QKeySequence("Ctrl+S"));
    saveImage->setIcon(QIcon("image/saveImage.png"));
    deleteImage = menuImages->addAction("Supprimer image");
    deleteImage->setShortcut(QKeySequence("Ctrl+D"));
    deleteImage->setIcon(QIcon("image/deletImage.png"));
    // Selection menu
    selectROI = menuSelect->addAction("Selectionner &Zone");
    selectROI->setShortcut(QKeySequence("Ctrl+R"));
    selectROI->setIcon(QIcon("image/roi.png"));
    selectROI->setCheckable(true);
    selectRect = menuSelect->addAction("Dessiner &Rectange");
    selectRect->setShortcut(QKeySequence("Ctrl+T"));
    selectRect->setIcon(QIcon("image/rect.png"));
    selectRect->setCheckable(true);
    selectCircle = menuSelect->addAction("Dessiner &Cercle");
    selectCircle->setShortcut(QKeySequence("Ctrl+Y"));
    selectCircle->setIcon(QIcon("image/circle.png"));
    selectCircle->setCheckable(true);
    selectLine = menuSelect->addAction("Dessiner &Ligne");
    selectLine->setShortcut(QKeySequence("Ctrl+L"));
    selectLine->setIcon(QIcon("image/line.png"));
    selectLine->setCheckable(true);
    // Help menu
    infoQt = menuHelp->addAction("A propos de &Qt...");
    infoQt->setIcon(QIcon("image/qt.png"));

    // File Bar
    barFichier = addToolBar("Fichier");
    barFichier->addAction(openVideo);
    barFichier->addAction(openConfig);
    barFichier->addAction(saveConfig);
    barFichier->addAction(quit);
    // Image bar
    barImage = addToolBar("Image");
    barImage->addAction(saveImage);
    barImage->addAction(deleteImage);
    // Selection bar
    barSelect = addToolBar("Selection");
    barSelect->addAction(selectROI);
    barSelect->addAction(selectRect);
    barSelect->addAction(selectCircle);
    barSelect->addAction(selectLine);

    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(openVideo, SIGNAL(triggered()), this, SLOT(openVideoDir()));

    connect(saveImage, SIGNAL(triggered()), this, SLOT(openSaveImage()));
    connect(deleteImage, SIGNAL(triggered()), this, SIGNAL(deleteCurrentImage()));

    connect(selectROI, SIGNAL(triggered()), this, SLOT(checkROI()));
    connect(selectRect, SIGNAL(triggered()), this, SLOT(checkRect()));
    connect(selectCircle, SIGNAL(triggered()), this, SLOT(checkCircle()));
    connect(selectLine, SIGNAL(triggered()), this, SLOT(checkLine()));

    connect(infoQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}

void MainWindow::openSaveImage()
{
    imageDir = QFileDialog::getSaveFileName(this, "Enregistrer une image", "image.png", "Images (*.png)");
    emit savedImage(imageDir);
}

void MainWindow::openVideoDir()
{
    videoDir = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Video (*.webw *.png *.avi *.*)");
    emit gettedVideoDir();
}

QString MainWindow::getVideoDir()
{
    return videoDir;
}

void MainWindow::checkROI()
{
    if(roiChecked)
    {
        roiChecked=false;
        selectROI->setChecked(false);
        emit drawShape(null);
    }
    else
    {
        roiChecked=true;
        selectROI->setChecked(true);
        rectChecked=false;
        selectRect->setChecked(false);
        circleChecked=false;
        selectCircle->setChecked(false);
        lineChecked=false;
        selectLine->setChecked(false);
        emit drawShape(ROI);
    }
}

void MainWindow::checkRect()
{
    if(rectChecked)
    {
        rectChecked=false;
        selectRect->setChecked(false);
        emit drawShape(null);
    }
    else
    {
        roiChecked=false;
        selectROI->setChecked(false);
        rectChecked=true;
        selectRect->setChecked(true);
        circleChecked=false;
        selectCircle->setChecked(false);
        lineChecked=false;
        selectLine->setChecked(false);
        emit drawShape(Rectangle);
    }
}

void MainWindow::checkCircle()
{
    if(circleChecked)
    {
        circleChecked=false;
        selectCircle->setChecked(false);
        emit drawShape(null);
    }
    else
    {
        roiChecked=false;
        selectROI->setChecked(false);
        rectChecked=false;
        selectRect->setChecked(false);
        circleChecked=true;
        selectCircle->setChecked(true);
        lineChecked=false;
        selectLine->setChecked(false);
        emit drawShape(Circle);
    }
}
void MainWindow::checkLine()
{
    if(lineChecked)
    {
        roiChecked=false;
        selectROI->setChecked(false);
        emit drawShape(null);
    }
    else
    {
        roiChecked=false;
        selectROI->setChecked(false);
        rectChecked=false;
        selectRect->setChecked(false);
        circleChecked=false;
        selectCircle->setChecked(false);
        lineChecked=true;
        selectLine->setChecked(true);
        emit drawShape(Line);
    }
}

void MainWindow::setDisplay(ImageDisplay *imgD, ImageAllDisplay *imgAD, ImageListDisplay *imgLD, SelectionDisplay *selD)
{
    imageD = imgD;
    this->setCentralWidget(imageD);

    rightDock = new QDockWidget("Selection", this);
    rightDock->setMinimumWidth(200);
    addDockWidget(Qt::RightDockWidgetArea, rightDock);

    bottomDock = new QDockWidget("Navigation", this);
    bottomDock->setMinimumHeight(100);
    addDockWidget(Qt::BottomDockWidgetArea, bottomDock);

    imageAllD = imgAD;
    imageListD = imgLD;
    selectD = selD;

    bottomDock->setWidget(imageListD);
}
